import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 形状接口
 */
interface Shape {
    public double perimeter(); // 计算形状的周长
    public double area(); // 计算形状的面积
}

/**
 * 参数为负数异常类
 */
class NegativeException extends Throwable {
    public NegativeException(String s) {
        super(s);
    }
}

/**
 * 圆型类
 */
class Circle implements Shape{

    private double radius;

    public Circle(double re) throws NegativeException{
        if (re<0){
         //   System.out.println("圆不能为负数");
            throw new NegativeException("圆的班级不能为负数");
        }
        this.radius=re;


    }


    public double getRadius() {
        return radius;
    }
    public void setRadius(double rd) {
        this.radius=rd;
    }

    @Override
    public double perimeter() {
        return 2*Math.PI*radius;
    }

    @Override
    public double area() {
        return Math.PI*radius*radius;
    }
}

/**
 * 正方形类
 */
class Square implements Shape{
    private double bc;

    public Square(double re) throws NegativeException{
        if(re<0){
            throw new NegativeException("正方形边长不能为负数");
        }
        this.bc=re;
    }


    public double getRadius() {
        return bc;
    }
    public void setRadius(double rd) {
        this.bc=rd;
    }

    @Override
    public double perimeter() {
        return 4*bc;
    }

    @Override
    public double area() {
        return bc*bc;
    }

}

/**
 * 三角形三边不合法异常类
 */
class InvalidTriangleException extends RuntimeException {
        public InvalidTriangleException(String message){
            super(message);
        }

}

/**
 * 三角形类
 */
class Triangle implements Shape {

    public double di;
    public double er;
    public double san;

    public Triangle(double a,double b,double c) throws InvalidTriangleException,NegativeException  {
        if( a<=0 || b<=0 || c<=0 ){
            throw new NegativeException("三角形边长不能小于0");
        }
        if( a+b<=c || a+c<=b || b+c<=a ){
            throw new InvalidTriangleException("三角形边长不能构成边长");
        }
        this.di=a;
        this.er=b;
        this.san=c;

    }

    public void setTriangle(double a, double b, double c) {
        this.di=a;
        this.er=b;
        this.san=c;
    }


    @Override
    public double perimeter() {
        double he=di+er+san;
        return he;
    }


    @Override
    public double area() {
        double s = (di + er + san) / 2;
        return Math.sqrt(s * (s - di) * (s - er) * (s - san));
    }

}

/**
 * 矩形类
 */
class Rectangle implements Shape {
    public double cha;
    public double dua;
    public Rectangle(double a,double b){
        this.cha=a;
        this.dua=b;
    }
    public void setTriangle(double a, double b, double c) {
        this.cha=a;
        this.dua=b;
    }
    @Override
    public double perimeter() {
        double he=this.cha+this.dua;
        return 2*he;
    }

    @Override
    public double area() {
        return cha*dua;
    }
}

/**
 * 椭圆型类
 */
class Ellipse implements Shape {
    private double sma;
    private double sna;

    public Ellipse(double semiMajorAxis, double semiMinorAxis) throws NegativeException {
        if (semiMajorAxis < 0 || semiMinorAxis < 0) {
            throw new NegativeException("椭圆的半轴长不能为负数");
        }
        this.sma = semiMajorAxis;
        this.sna = semiMinorAxis;
    }

    @Override
    public double perimeter() {
        // 使用拉马努金近似公式计算椭圆周长
        double h = Math.pow((sma - sna) / (sma + sna), 2);
        return Math.PI * (sma + sna) * (1 + 3 * h / (10 + Math.sqrt(4 - 3 * h)));
    }

    @Override
    public double area() {
        return Math.PI * sma * sna;
    }
}

public class HW2 {
    public static void javaClassInterface() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入第1个浮点数: ");
        double value1 = 0;
        double value2 = 0;
        double value3 = 0;
        try {
            value1=scanner.nextDouble();
            value2=scanner.nextDouble();
            value3=scanner.nextDouble();
        }catch (InputMismatchException e){
            System.out.println(e.getMessage());
        }

       //圆
        try {
            Circle a= new Circle(value1);
       //     Square b= new Square(value1);
       //    Triangle c = new Triangle(value1,value2,value3);
            System.out.println(a.getRadius());
            System.out.printf("圆的周长为：%.2f",a.perimeter());
            System.out.println();
            System.out.printf("圆的面积为：%.2f",a.area());
            System.out.println();

        } catch (NegativeException e){
            System.out.println(e.getMessage());
        }
        //正方形
        try {
            //Circle a= new Circle(value1);
            Square b= new Square(value1);
            //    Triangle c = new Triangle(value1,value2,value3);
            System.out.println(b.getRadius());
            System.out.printf("正方形的周长为：%.2f",b.perimeter());
            System.out.println();
            System.out.printf("正方形的面积为：%.2f",b.area());
            System.out.println();

        } catch (NegativeException e){
            System.out.println(e.getMessage());
        }

        //三角形
        try {
            //Circle a= new Circle(value1);
            //     Square b= new Square(value1);
                Triangle c = new Triangle(value1,value2,value3);
         //   System.out.println(c.getRadius());
            System.out.printf("三角形的周长为：%.2f",c.perimeter());
            System.out.println();
            System.out.printf("三角形的面积为：%.2f",c.area());
            System.out.println();

        } catch (InvalidTriangleException e){
            System.out.println(e.getMessage());
        }catch (NegativeException e){
            System.out.println(e.getMessage());
        }






//        System.out.printf("正方形的周长为：%.2f",b.perimeter());
//        System.out.println();
//        System.out.printf("正方形的面积为：%.2f",b.area());
//        System.out.println();





    }

    public static void wordFrequencyStatistics() {
        LinkedHashMap<String,Integer> wordmap=new LinkedHashMap<>();
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader("src/text.txt"))){
                String line;
                while ((line=bufferedReader.readLine())!=null){
                    ArrayList<String> words= extractWords(line);
                    for(String s:words){
                        String ss = s.toLowerCase();
                      //  System.out.println(ss);
                        if(!wordmap.containsKey(ss)){
                            wordmap.put(ss,0);
                        }
                        int shu=wordmap.get(ss);
                        wordmap.replace(ss,shu+1);
                    }
                }
        }catch (IOException e){
            System.out.println(e.getMessage());
        }
//
//        for (Map.Entry<String, Integer> entry : wordmap.entrySet()) {
//            String key = entry.getKey();
//            Integer val = entry.getValue();
//
//          System.out.println(key + ": " + val);
//
//        }
//        int ind=0;
//          while(ind<5){
//              for (Map.Entry<String, Integer> entry : wordmap.entrySet()) {
//                  String key = entry.getKey();
//                  Integer val = entry.getValue();
//                  int ma=-1;
//                  for(Integer s:wordmap.values()){
//                      if (s>ma){
//                          ma=s;
//                      }
//                  }
//                  if(ma==val){
//                      System.out.println(key + ": " + val);
//                      wordmap.replace(key,val,0);
//                      ind++;
//                      break;
//                  }
//              }
//        }
        List<Map.Entry<String, Integer>> list = new ArrayList<>(wordmap.entrySet());
//        list.sort(new Comparator<Map.Entry<String, Integer>>() {
//            @Override
//            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
//                return o2.getValue().compareTo(o1.getValue());
//            }
//        });
        list.sort((o1, o2) -> o2.getValue().compareTo(o1.getValue()));
        LinkedHashMap<String, Integer> sortedMap = new LinkedHashMap<>();
        for (Map.Entry<String, Integer> entry : list) {
            sortedMap.put(entry.getKey(), entry.getValue());
        }
        int ind=1;
        for (Map.Entry<String, Integer> entry : sortedMap.entrySet()) {
            System.out.println(entry.getKey() + ": " + entry.getValue());
            if (ind==5){
                break;
            }
            ind++;
        }















          //---
    }

    public static void main(String[] args) {
        javaClassInterface();
       //wordFrequencyStatistics();
    }

    public static ArrayList<String> extractWords(String text) {
        ArrayList<String> words = new ArrayList<>();
        // 匹配字母和撇号的组合（允许单词中间有“'”）
        Pattern pattern = Pattern.compile("[a-zA-Z']+");
        Matcher matcher = pattern.matcher(text);

        while (matcher.find()) {
            String word = matcher.group();
            // 清理单词前后的非字母字符（如标点）
            word = word.replaceAll("^[^a-zA-Z]+", "");  // 去除开头非字母
            word = word.replaceAll("[^a-zA-Z]+$", "");  // 去除结尾非字母
            if (!word.isEmpty()) {
                words.add(word);
            }
        }

        return words;
    }
}
